/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network.packets;

import datamodel.Message;
import utils.StringUtils;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class MessageResponce extends Packet
{
    private Message message;
    
    public MessageResponce(byte[] data)
    {
        super(MESSAGE_RESPONCE, data);
    }
    
    public void parse(byte[] data)
    {   
        message = new Message();

        int offset = 2;

        //Заголовок
        byte[] tmp = new byte[4];
        System.arraycopy(data, offset, tmp, 0, tmp.length);
        offset += tmp.length;
        tmp = new byte[Utils.byteToInt(tmp)];
        System.arraycopy(data, offset, tmp, 0, tmp.length);
        offset += tmp.length;
        message.setTitle(StringUtils.bytesToStr(tmp));

        //Тело сообщения
        tmp = new byte[4];
        System.arraycopy(data, offset, tmp, 0, tmp.length);
        offset += tmp.length;
        tmp = new byte[Utils.byteToInt(tmp)];
        System.arraycopy(data, offset, tmp, 0, tmp.length);
        offset += tmp.length;
        message.setText(StringUtils.bytesToStr(tmp));
        
        tmp = null;
    }

    public void print()
    {
        System.out.println("MessageResponce={" + message.getTitle() + " : " + message.getText() + "}");
    }

    public Message getMessage() 
    {
        return message;
    }
}
